package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojo.ApUser;
import com.heima.model.user.pojo.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.heima.wemedia.feign.WemediaClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    public ApUserRealnameServiceImpl(IWemediaClient wemediaClient, ApUserMapper apUserMapper) {
        this.wemediaClient = wemediaClient;
        this.apUserMapper = apUserMapper;
    }


    /**
     * 用户审核列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult userScan(AuthDto dto) {
        //参数校验 page  size
        if (dto.getPage() != null && dto
                .getSize() != null) {

            IPage page = new Page(dto.getPage(), dto.getSize());

            LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
            if (dto.getStatus() != null) {
                wrapper.eq(ApUserRealname::getStatus, dto.getStatus());
            }
            wrapper.orderByDesc(ApUserRealname::getCreatedTime);

            page = page(page, wrapper);

            ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());

            responseResult.setData(page.getRecords());

            return responseResult;
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    @Override
    public ResponseResult failScan(AuthDto dto) {

        if (dto != null) {
            log.info("dto校验有效...");
            if (null != dto.getId()) {
                log.info("ApUserRealnameId校验有效...");
                ApUserRealname one = this.getOne(Wrappers.
                        <ApUserRealname>lambdaQuery().eq(ApUserRealname::getId, dto.getId()));

                one.setStatus(2);
                one.setReason(dto.getMsg());
                updateById(one);
                return PageResponseResult.errorResult(200, dto.getMsg());
            }
        return PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return PageResponseResult.errorResult(501,"参数错误");
    }
    private final IWemediaClient wemediaClient;
    private final ApUserMapper apUserMapper;

    @Override
    public ResponseResult passScan(AuthDto dto) {
        if (dto != null) {
            log.info("dto校验有效...");
       if (null != dto.getId()) {
            log.info("ApUserRealnameId校验有效...");
            ApUserRealname one = this.getOne(Wrappers.
                    <ApUserRealname>lambdaQuery().eq(ApUserRealname::getId, dto.getId()));

            one.setStatus(9);
            updateById(one);

            //远程调用新建自媒体账号
            ApUser apUser = apUserMapper.selectById(one.getUserId());
            wemediaClient.addWmUser(apUser);


            return PageResponseResult.errorResult(200, dto.getMsg());
        }
            return PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return PageResponseResult.errorResult(501,"参数错误");

    }
}

